package cn.jtfadmin.base.doc.swagger;

import cn.jtfadmin.base.lang.config.module.ModuleInfo;
import org.springframework.util.StringUtils;
import org.springframework.web.context.request.async.DeferredResult;
import springfox.documentation.RequestHandler;
import springfox.documentation.builders.PathSelectors;
import springfox.documentation.builders.RequestHandlerSelectors;
import springfox.documentation.service.ApiInfo;
import springfox.documentation.spi.DocumentationType;
import springfox.documentation.spi.service.DocumentationPlugin;
import springfox.documentation.spi.service.contexts.DocumentationContext;
import springfox.documentation.spi.service.contexts.DocumentationContextBuilder;
import springfox.documentation.spring.web.plugins.Docket;

import java.util.ArrayList;
import java.util.Set;
import java.util.function.Predicate;

import static cn.jtfadmin.base.doc.swagger.SwaggerConfig.publicParam;


public class DockerProxy implements DocumentationPlugin {

    protected Docket docker;

    public DockerProxy(ModuleInfo moduleInfo){
        String basePackage = moduleInfo.getBasePackage();
        Predicate<RequestHandler> predicate = null;
        if(StringUtils.hasText(basePackage)){
            Set<String> strings = StringUtils.commaDelimitedListToSet(basePackage);
            for (String string : strings) {
                if(predicate == null){
                    predicate =  RequestHandlerSelectors.basePackage(string);
                }else {
                    predicate = predicate.or(RequestHandlerSelectors.basePackage(string));
                }
            }
        }
        if(predicate == null){
            predicate = RequestHandlerSelectors.any();
        }
        this.docker =  new Docket(DocumentationType.OAS_30)
                .groupName(moduleInfo.getName())
                .globalRequestParameters(publicParam)
                .genericModelSubstitutes(DeferredResult.class)
                .useDefaultResponseMessages(false)
                .forCodeGeneration(false)
                .pathMapping("/")
                .select()
                .apis(predicate)
                .paths(PathSelectors.any())
                .build()
                .apiInfo(new ApiInfo(
                        moduleInfo.getName(),
                        moduleInfo.getDescribe(),
                        "1.0",
                        "urn:tos",
                        ApiInfo.DEFAULT_CONTACT,
                        "Apache 2.0",
                        "http://www.apache.org/licenses/LICENSE-2.0",
                        new ArrayList<>()));
    }

    @Override
    public boolean isEnabled() {
        return docker.isEnabled();
    }

    @Override
    public DocumentationType getDocumentationType() {
        return docker.getDocumentationType();
    }

    @Override
    public DocumentationContext configure(DocumentationContextBuilder builder) {
        return docker.configure(builder);
    }

    @Override
    public String getGroupName() {
        return docker.getGroupName();
    }

    @Override
    public boolean supports(DocumentationType delimiter) {
        return docker.supports(delimiter);
    }
}
